package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.Event;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.AddDatabasePresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IAddDatabaseView;
import com.konne.nightmare.FastPublicOpinion.ui.AddDataHelper;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.ImportTypePopup;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.LocalUploadDialog;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.TimeSelDialog;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.DatabaseImportView;
import com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView;
import com.konne.nightmare.FastPublicOpinion.widget.QJRadioGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/2/12  Time:下午 5:11.
 * description: 添加考试 .
 */
public class AddExamAct extends BaseMvpActivity<IAddDatabaseView, AddDatabasePresenter> implements IAddDatabaseView, View.OnClickListener, QJItemTitleView.OnCheckedChangeListener {
    @BindView(R.id.itv_timing_time)
    QJItemTitleView itv_timing_time;
    @BindView(R.id.importView)
    DatabaseImportView importView;
    private AddDataHelper dataHelper;

    public static void start() {
        ActivityUtils.startActivity(AddExamAct.class);
    }

    @Override
    protected AddDatabasePresenter createPresenter() {
        return new AddDatabasePresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.act_add_exam;
    }

    @Override
    public void initView() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
        dataHelper = new AddDataHelper(this, findViewById(R.id.pic_show), findViewById(R.id.ll_data));

        importView.setDatabaseImportListener(new DatabaseImportView.DatabaseImportListener() {
            @Override
            public void databaseImport(int type, boolean isSel) {
                switch (type) {
                    case 0:
                        new LocalUploadDialog(AddExamAct.this).setLocalUploadListener(new LocalUploadDialog.LocalUploadListener() {
                            @Override
                            public void localUpload(int type) {
                                dataHelper.showLocalUpload(type);
                            }
                        }).show();
                        break;
                    case 1:
                        ImportDatabaseAct.start(1);
                        break;
                    case 2:
                        findViewById(R.id.cl_input_link).setVisibility(isSel?View.VISIBLE:View.GONE);
                        break;
                }
            }
        });
        QJRadioGroup radioGroup = findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new QJRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJRadioGroup var1, int var2) {
                radioGroup.getChildAt(2).setVisibility(var2 == 1 ? View.VISIBLE : View.GONE);
                radioGroup.getChildAt(3).setVisibility(var2 == 1 ? View.VISIBLE : View.GONE);
            }
        });
        ((QJItemTitleView) findViewById(R.id.itv_send_class)).setOnCheckedChangeListener(this);
        ((QJItemTitleView) findViewById(R.id.itv_timing)).setOnCheckedChangeListener(this);
        findViewById(R.id.itv_section).setOnClickListener(this);
        findViewById(R.id.itv_start_stop).setOnClickListener(this);
        findViewById(R.id.itv_setting).setOnClickListener(this);
        findViewById(R.id.ll_end_time).setOnClickListener(this);
        findViewById(R.id.ll_start_time).setOnClickListener(this);

        itv_timing_time.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itv_start_stop:
                View ll_start_stop = findViewById(R.id.ll_start_stop);
                SuperTextView tv_start_stop = findViewById(R.id.tv_start_stop);
                if (ll_start_stop.getVisibility() == View.VISIBLE) {
                    ll_start_stop.setVisibility(View.GONE);
                    tv_start_stop.setDrawableRotate(-90);
                } else {
                    ll_start_stop.setVisibility(View.VISIBLE);
                    tv_start_stop.setDrawableRotate(90);
                }
                break;
            case R.id.itv_timing_time:
                new TimeSelDialog(this).setTimeSelectListener(new TimeSelDialog.TimeSelectListener() {
                    @Override
                    public void timeSelect(String str) {
                        TextView tv_sent_time = findViewById(R.id.tv_sent_time);
                        tv_sent_time.setText(str);
                    }
                }).show();
                break;
            case R.id.itv_section:
                ImportDatabaseAct.start(0);
                break;
            case R.id.itv_setting:
                ExamSettingAct.start();
                break;
            case R.id.ll_start_time:
                showTimeSel((ViewGroup) view,true);
                break;
            case R.id.ll_end_time:
                showTimeSel((ViewGroup) view,false);
                break;
        }
    }
    private void showTimeSel(ViewGroup vp,boolean isStart) {
        Calendar selectedDate = Calendar.getInstance();
        //设置最小日期和最大日期
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();//最大日期是今天
        startDate.set(1950, 0, 1);
        //时间选择器
        TimePickerView mStartDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                String text = TimeUtils.date2String(date, "yyyy-MM-dd hh:mm");
                TextView tv= (TextView) vp.getChildAt(0);
                tv.setText(text);
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "")
                .isCenterLabel(false)//是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setTitleText(isStart?"开始时间":"截止时间")//标题文字
                .setTitleSize(20)//标题文字大小
                .setContentTextSize(20)//滚轮文字大小
                .setLineSpacingMultiplier(1.8f)//行间距
                .setRangDate(startDate, endDate)//设置最小和最大日期
                .setDate(selectedDate)//设置选中的日期
                .build();
        mStartDatePickerView.show();
    }
    @Override
    public void showClass(List<String> classs) {

        ViewGroup ll_send_sent = findViewById(R.id.ll_send_sent);
        LinearLayout classCon = (LinearLayout) ll_send_sent.getChildAt(1);
        classCon.removeAllViews();
        for (int i = 0; i < classs.size(); i++) {
            classCon.addView(getClassItem(classs.get(i)));
        }
    }

    private View getClassItem(String cl) {
        QJItemTitleView titleView = new QJItemTitleView(this);
        titleView.setTitle(cl);
        titleView.setTitleSize(12);
        titleView.setTitleColor(0xff67788A);
        titleView.setStyle(QJItemTitleView.Style.check);
        ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(40));
        titleView.setLayoutParams(params);
        return titleView;
    }

    @Override
    public void onCheckedChanged(QJItemTitleView titleView, QJItemTitleView.Style style, boolean isChecked) {
        switch (titleView.getId()) {
            case R.id.itv_send_class:
                findViewById(R.id.ll_send_sent).setVisibility(isChecked ? View.VISIBLE : View.GONE);
                if (isChecked) {
                    mPresenter.showClass();
                }
                break;
            case R.id.itv_timing:
                itv_timing_time.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEvent(Event.AddDataItem dataItem) {
        dataHelper.addDataItem();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        dataHelper.onActivityResult(requestCode, resultCode, data);
    }
}
